package controller

import (
	"github.com/gin-gonic/gin"

	"gin-app/model"
	"gin-app/utils"
	"gin-app/utils/response"
)

func Info(ctx *gin.Context) {
	//var user []model.User
	user := model.QueryUser()
	response.Success(ctx, gin.H{
		"data": user,
	})

	return
}

func ErrTest(ctx *gin.Context) {
	//var user []model.User
	response.Fail(ctx, "错误信息测试")

	return
}
func FindUser(c *gin.Context) {
	name := c.Query("name")
	b, err := model.FindUser(name)
	response.Success(c, gin.H{
		"id":  b,
		"err": err,
	})

	return

}

func DelUser(c *gin.Context) {
	id := c.Query("id")
	i, err := model.DelUser(utils.StringToInt64(id))
	response.Success(c, gin.H{
		"id":  i,
		"err": err,
	})

}

// Register 用户注册账号
func Register(c *gin.Context) {
	// 1.获取前端传递过来的数据
	var registerDto model.Users
	// if err := c.ShouldBindJSON(&registerDto); err != nil {
	// 	// 2.校验数据是否合法
	// 	message := utils.ShowErrorMessage(err)
	// 	utils.Fail(c, message)
	// 	return
	// }

	if c.ShouldBind(&registerDto) == nil {
		passwd, _ := utils.GeneratePassword(registerDto.UserPassWord)
		err := model.InsertUser(model.Users{UserName: registerDto.UserName, UserPassWord: passwd})
		if err != nil {

			response.Fail(c, "注册失败"+err.Error())
			return
		}

		response.Success(c, gin.H{
			"data": registerDto,
		})

		return

	}

}
